-- slime_global_damage
-- create by zhuangsl
-- 技能：全体伤害。对地牢中所有显性怪物造成伤害
-- 目前支持如下两种配置：
-- 1、slime_global_damage(5, 300, 300)：对所有怪物造成30% + attack * (level * 0.3 + base) 的伤害
-- 2、slime_global_damage(5, 300, 300, 16, 1000)：对所有怪物造成30% + attack * (level * 0.3 + base) 的伤害;若目标是人类（类型编号16），则伤害提升100%

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 专属技能等级
        local level;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            local prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        local base = 700;
        local force = para[3];

        -- 攻击方的攻击
        local attack = source:getAttack();

        -- 伤害
        local damage = math.modf(attack * (base + force * level) / 1000);

        -- 斯巴达专属加成
        local prop = PropM.combine(source, "battle_cry", 1);
        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end
        damage = PropM.apply(prop, damage);

        -- 遍历所有的怪
        local ret = {};
        local monster;

        for pos = 1, GRID_SIZE do
            repeat
                local grid = DungeonM.getGridByPos(pos);
                if not grid:isOpened() or
                   not grid:isMonster() or
                   not grid.monster or
                   grid.monster:isDead() then
                   break;
                end

                local monster = grid.monster;
                table.insert(ret, monster);

                local maxHp = monster:getHp();
                local finalDamage = damage + math.modf(maxHp * para[2] / 1000);
                if #para == 5 and para[4] == monster.style then
                    -- 系别加成或削减
                    finalDamage = finalDamage + math.modf(para[5] * damage / 1000);
                end
                if monster:isBoss() then
                    -- 对BOSS伤害减半
                    finalDamage = math.modf(finalDamage / 2);
                end

                -- 增加命中动作
                if SkillM.getSequence(monster) == nil then
                    SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
                end

                SkillM.getSequence(monster):hit(source, monster, skillId);

                -- 受创
                CombatM.receiveDamage(source, monster, finalDamage, skillId, extra);

            until true;
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        if (#arr == 3) then
            return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3])};
        else
            return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), tonumber(arr[5]), };
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 专属技能等级
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = PropM.apply(prop, 0);

        local base = 700;
        local force = para[3];

        -- 攻击方的攻击
        local attack = source:getAttack();

        -- 伤害
        local damage = math.modf(attack * (base + force * level) / 1000);
        
        -- 斯巴达专属加成
        local prop = PropM.combine(source, "battle_cry", 1);
        damage = PropM.apply(prop, damage);

        desc = string.gsub(desc, "{damage}", damage);

        -- 百分比部分的伤害
        local damageRate = para[2] / 10;

        desc = string.gsub(desc, "{damage_rate}", damageRate);

        return desc;
    end,
};
